package com.aigc.config;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class KnowledgeBase {

    @Autowired
    private VectorStore vectorStore;

    public void getKnowledgeBase() {
        String jsonString = "[{\"id\": \"rule_01\", \"type\": \"合约期\", \"condition\": \"合约期未满12个月\", \"description\": \"提前更换套餐需支付剩余费用的10%作为违约金。\"}, {\"id\": \"rule_02\", \"type\": \"携号转网\", \"condition\": \"申请携号转网\", \"description\": \"需满足：1. 无欠费 2. 当前套餐已到期 3. 非吉祥号段（如138/139开头）。\"}]";
        JSONArray jsonArray = JSONUtil.parseArray(jsonString);
        List<Document> documents = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            // 将JSON对象的字段组合成文本内容
            String content = "类型: " + jsonObject.getStr("type") +
                    "\n条件: " + jsonObject.getStr("condition") +
                    "\n描述: " + jsonObject.getStr("description");

            // 创建元数据
            Map<String, Object> metadata = new HashMap<>();
            metadata.put("id", jsonObject.getStr("id"));
            metadata.put("type", jsonObject.getStr("type"));
            metadata.put("condition", jsonObject.getStr("condition"));

            // 创建Document对象
            Document document = new Document(content, metadata);
            documents.add(document);
        }
        vectorStore.add(documents);

    }


}
